package com.efamily.ewallet.client;

import java.util.List;

import com.efamily.ewallet.client.account.AccountsWidget;
import com.efamily.ewallet.client.account.RegisterAccountWindow;
import com.efamily.ewallet.client.account.bean.AccountBean;
import com.efamily.ewallet.client.common.CustomEventType;
import com.efamily.ewallet.client.common.CustomEvents;
import com.efamily.ewallet.client.common.bean.UserBean;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.Image;

public class MainWidget extends Composite {
	private ContentPanel mainPanel = new ContentPanel();
	private ContentPanel northPanel = new ContentPanel();
	private ContentPanel centerPanel = new ContentPanel();
	public static UserBean currentUser = null;
	private LoginWidget loginWidget = new LoginWidget() {
		@Override
		protected void loginSuccess(UserBean result) {
			currentUser = result;
			Dispatcher.get().fireEvent(
					CustomEvents.LOGED_IN.getEventType());
		}
	};

	public MainWidget() {
		mainPanel.setHeaderVisible(false);
		mainPanel.setLayout(new BorderLayout());
		mainPanel.setBodyBorder(false);
		buildNorthPanel();
		buildCenterPanel();
		initComponent(mainPanel);
		createMainMenu();
	}

	private void buildNorthPanel() {
		northPanel.setLayout(new BorderLayout());
		northPanel.setHeaderVisible(false);
		ContentPanel logoPanel = new ContentPanel();
		logoPanel.setHeaderVisible(false);
		logoPanel.setBodyBorder(false);
		//Image logoImg = new Image("images/logo.jpg");
		//logoPanel.add(logoImg);
		logoPanel.add(new Html("<font size='100%' color='navy'><i>E-Wallet</i></font>"));
		northPanel.add(logoPanel, new BorderLayoutData(LayoutRegion.CENTER));
		BorderLayoutData eastPanelData = new BorderLayoutData(
				LayoutRegion.EAST, 250);
		configLoginPanel();
		northPanel.add(loginWidget, eastPanelData);
		mainPanel
				.add(northPanel, new BorderLayoutData(LayoutRegion.NORTH, 100));
	}

	private void configLoginPanel() {
		Button registerBtn = new Button("Register");
		registerBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				RegisterAccountWindow.getInstance().show();
			}
		});
		loginWidget.insertButton(registerBtn, 0);
	}

	private void buildCenterPanel() {
		final TabPanel tabPanel = new TabPanel();
		final TabItem accountListTab = new TabItem("User List");
		accountListTab.setLayout(new FitLayout());
		accountListTab.setClosable(true);
		accountListTab.add(new AccountsWidget());
		Dispatcher.get().addListener(
				CustomEvents.SHOW_USERS_LIST.getEventType(),
				new Listener<BaseEvent>() {
					public void handleEvent(BaseEvent be) {
						if (tabPanel.getItem(0) != accountListTab) {
							tabPanel.insert(accountListTab, 0);
						}
						tabPanel.setSelection(accountListTab);
					}
				});

		Dispatcher.get().addListener(
				CustomEvents.SHOW_USER_BUSINESS.getEventType(),
				new Listener<BaseEvent>() {
					@SuppressWarnings("unchecked")
					public void handleEvent(BaseEvent be) {
						List<BeanModel> accounts = (List<BeanModel>) be
								.getSource();
						boolean activeTab = (accounts.size() == 1) ? true
								: false;
						for (BeanModel account : accounts) {
							AccountBean accountBean = account.getBean();
							TabItem walletTab = new TabItem(accountBean
									.getNickName()
									+ " Wallet");
							walletTab.setLayout(new FitLayout());
							walletTab.setClosable(true);
							walletTab.add(new BussinessWidget(accountBean));
							tabPanel.add(walletTab);
							if (activeTab)
								tabPanel.setSelection(walletTab);
						}
					}
				});

		tabPanel.setBodyBorder(false);
		tabPanel.add(accountListTab);

		BorderLayoutData centerPanelData = new BorderLayoutData(
				LayoutRegion.CENTER);
		centerPanel.add(tabPanel);
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new FitLayout());
		centerPanelData.setSplit(true);
		mainPanel.add(centerPanel, centerPanelData);
	}

	private void createMainMenu() {
		Menu accountMenu = new Menu();
		final MenuItem createAccountItem = new MenuItem("Create New Account");
		accountMenu.add(createAccountItem);
		createAccountItem
				.addSelectionListener(new SelectionListener<MenuEvent>() {
					@Override
					public void componentSelected(MenuEvent ce) {
						RegisterAccountWindow.getInstance().show();
					}
				});
		final MenuItem editAccountItem = new MenuItem("Edit my Account");
		editAccountItem.setVisible(false);
		accountMenu.add(editAccountItem);
		final MenuItem deleteAccountItem = new MenuItem("Delete my Account");
		deleteAccountItem.setVisible(false);
		accountMenu.add(deleteAccountItem);

		Button accountBtn = new Button("Account");
		accountBtn.setMenu(accountMenu);

		final Button logoutBtn = new Button("Logout");
		logoutBtn.setVisible(false);
		logoutBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.get().fireEvent(
						CustomEvents.LOGED_OUT.getEventType());
			}
		});

		Menu viewMenu = new Menu();
		MenuItem userListItem = new MenuItem("Show Users List");
		userListItem.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				Dispatcher.get().fireEvent(
						CustomEvents.SHOW_USERS_LIST.getEventType());
			}
		});
		MenuItem userDetailsItem = new MenuItem("Show User Details");
		userDetailsItem
				.addSelectionListener(new SelectionListener<MenuEvent>() {
					@Override
					public void componentSelected(MenuEvent ce) {
						Dispatcher.get().fireEvent(
								CustomEvents.SHOW_USER_DETAILS.getEventType());
					}
				});

		viewMenu.add(userListItem);
		viewMenu.add(userDetailsItem);
		final MenuItem balanceItem = new MenuItem("Show My Balance");
		balanceItem.setVisible(false);
		viewMenu.add(balanceItem);
		final MenuItem profileItem = new MenuItem("Show My Profile");
		profileItem.setVisible(false);
		viewMenu.add(profileItem);

		Button viewBtn = new Button("View");
		viewBtn.setMenu(viewMenu);

		Menu serviceMenu = new Menu();
		MenuItem currencyItem = new MenuItem("Currency Converter");
		serviceMenu.add(currencyItem);
		Button serviceBtn = new Button("Services");
		serviceBtn.setMenu(serviceMenu);

		Button helpBtn = new Button("Help");
		final ToggleButton hideShowBtn = new ToggleButton("Hide logo");
		hideShowBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (hideShowBtn.isPressed()) {
					hideShowBtn.setText("Show logo");
					BorderLayout layout = (BorderLayout) mainPanel.getLayout();
					layout.hide(LayoutRegion.NORTH);
				} else {
					hideShowBtn.setText("Hide logo");
					BorderLayout layout = (BorderLayout) mainPanel.getLayout();
					layout.show(LayoutRegion.NORTH);
				}
			}
		});

		Dispatcher.get().addListener(
				CustomEvents.LOGED_IN.getEventType(),
				new Listener<BaseEvent>() {
					public void handleEvent(BaseEvent be) {
						logoutBtn.setVisible(true);
						editAccountItem.setVisible(true);
						deleteAccountItem.setVisible(true);
						balanceItem.setVisible(true);
						profileItem.setVisible(true);
					}
				});
		Dispatcher.get().addListener(
				CustomEvents.LOGED_OUT.getEventType(),
				new Listener<BaseEvent>() {
					public void handleEvent(BaseEvent be) {
						logoutBtn.setVisible(false);
						editAccountItem.setVisible(false);
						deleteAccountItem.setVisible(false);
						balanceItem.setVisible(false);
						profileItem.setVisible(false);
					}
				});

		ToolBar bar = new ToolBar();
		bar.setBorders(true);
		bar.setStyleAttribute("borderTop", "none");
		bar.add(accountBtn);
		bar.add(new SeparatorToolItem());
		bar.add(viewBtn);
		bar.add(new SeparatorToolItem());
		bar.add(serviceBtn);
		bar.add(new SeparatorToolItem());
		bar.add(helpBtn);
		bar.add(new SeparatorToolItem());
		bar.add(hideShowBtn);
		bar.add(new SeparatorToolItem());
		bar.add(logoutBtn);

		centerPanel.setTopComponent(bar);
	}

}
